//
//  Question.m
//  caitu
//
//  Created by 楼楚 on 15/10/22.
//  Copyright © 2015年 Chu Lou. All rights reserved.
//

#import "Question.h"

@implementation Question
//1.所有方法的首字母小写
//2.单词切换时 单词首字母大写（驼峰法）
//3.类名要大写
- (instancetype)initWithDict:(NSDictionary *)dict

{
    self = [super init];
    if (self) {
        [self setValuesForKeysWithDictionary:dict];
//        对备选按钮进行乱序
        [self randomOptions];
    }
    return self;
}
+ (instancetype)questionWithDict:(NSDictionary *)dict
{
    return [[self alloc]initWithDict:dict];
}
+ (NSArray *)questions
{
    NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"questions.plist" ofType:nil]];
    NSMutableArray *arrayM = [NSMutableArray array];
    for (NSDictionary *dict in array) {
        [arrayM addObject:[self questionWithDict:dict]];
    }
    return arrayM;
}

- (void) randomOptions{
//    对options数组乱序
    self.options =[self.options sortedArrayUsingComparator:^NSComparisonResult(NSString   *str1, NSString   *str2) {
        int seed = arc4random_uniform(2);
        if (seed) {
            return [str1 compare:str2];
        }else{
            return [str2 compare:str1];
        }
        
    }];
    
}

//  对象描述方法，类似于java中的toString(),便于跟踪调试
- (NSString *)description
{
    return [NSString stringWithFormat:@"<%@: %p>{answer: %@,icon: %@,title: %@,options: %@}",self.class,self,self.answer,self.icon,self.title,self.options];
}

@end
